package com.lhh.controller;

import cn.hutool.core.convert.Convert;
import com.lhh.core.JsonResult;
import com.lhh.core.Result;
import com.lhh.entity.User;
import com.lhh.service.UserService;
import com.lhh.utils.JwtUtil;
import com.lhh.utils.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:
 */

@Api(description = "用户接口")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("test")
    @ResponseBody
    public String getData() {
        return "String";
    }

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @RequestMapping(value = "/login/register", method = RequestMethod.POST)
    @CrossOrigin
    public Map<String, Object> register(@RequestParam String userName,
                                        @RequestParam String passwd,
                                        @RequestParam String email,
                                        @RequestParam String telphone) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passwd)) {
            map.put("msg", "用户名和密码不能为空");
            return map;
        }
        // 验证用户名是否已经注册
        User exsitUser = userService.selectByName(userName);
        if (exsitUser != null) {
            map.put("msg", "该用户名已存在!");
            return map;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        User user = new User();
        user.setUserName(userName);
        user.setRegistTime(format);
        user.setStatus("1");
//        user.setPasswd(Md5Util.MD5(passwd) + userName);
        user.setPasswd(passwd);
        user.setTelphone(telphone);
        user.setEmail(email);
        int count = userService.insert(user);
//        System.out.println(count);
        if (count != 1) {
            map.put("msg", "注册失败");
            return map;
        }
        map.put("msg", "注册成功");
        return map;
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)

    public Result login(@RequestParam String userName,
                        @RequestParam String passwd,
                        HttpSession session,
                        HttpServletResponse response) {
        User exsitUser = userService.selectByName(userName);
        if (exsitUser == null) {
            return Result.fail("该用户未注册");
        }
        if (!exsitUser.getPasswd().equals(passwd)) {
            return Result.fail("密码错误，请重新输入");
        }
        session.setAttribute("loginUser", exsitUser);
        final Map<String, Object> map = Convert.toMap(String.class, Object.class, exsitUser);
        return Result.success("登录成功", JwtUtil.createJwt(map),exsitUser.getUserId());
    }

    @ApiOperation(value = "展示用户信息",notes = "展示用户信息")
    @RequestMapping(value = "/showinfo",method = RequestMethod.GET)
    @CrossOrigin
    public JsonResult<User> showInfo(Integer uid) {
        //从session中获取id
//        Integer id = Integer.valueOf(session.getAttribute("id").toString());

        User data = userService.getInfo(uid);
        String userName = data.getUserName();
        String tel = data.getTelphone();
        String email = data.getEmail();
//        System.out.println(userName+tel+email);
        return new JsonResult<>(1, userName,tel,email);
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    @CrossOrigin
    public Map<String, Object> UpdateUser(@RequestParam String userName,
                                          @RequestParam String email,
                                          @RequestParam String telphone) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(userName) ) {
            map.put("msg", "用户名不能为空");
            return map;
        }
        // 验证用户名是否已经注册
        User exsitUser = userService.selectByName(userName);
        if (exsitUser != null) {
            map.put("msg", "该用户名已存在!");
            return map;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        User user = new User();
        user.setUserName(userName);
        user.setUpdateTime(format);
        user.setStatus("1");
        user.setTelphone(telphone);
        user.setEmail(email);
        int count = userService.update(user);
        System.out.println(count);
        if (count != 1) {
            map.put("msg", "修改信息失败");
            return map;
        }
        map.put("msg", "修改信息成功");
        return map;
    }

}
